/**
  ******************************************************************************
  * @file    IAP_Main/Src/menu.c 
  * @author  MCD Application Team
  * @version 1.0.0
  * @date    8-April-2015
  * @brief   This file provides the software which contains the main menu routine.
  *          The main menu gives the options of:
  *             - downloading a new binary file, 
  *             - uploading internal flash memory,
  *             - executing the binary file already loaded 
  *             - configuring the write protection of the Flash sectors where the 
  *               user loads his binary file.
  * @note    2023/8/19/张剑勇。本文件为 STM32L031G6U6 进行了修改。
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */ 

/** @addtogroup STM32L0xx_IAP
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "common.h"
#include "flash_if.h"
#include "menu.h"
#include "ymodem.h"
#include "usart.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
pFunction JumpToApplication;
uint32_t JumpAddress;
//uint32_t FlashProtection = 0;
uint8_t aFileName[FILE_NAME_LENGTH];

/* Private function prototypes -----------------------------------------------*/
void SerialDownload(void);
void SerialUpload(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Download a file via serial port
  * @param  None
  * @retval None
  */
void SerialDownload(void)
{
//  uint8_t number[11] = {0};
  uint32_t size = 0;
  COM_StatusTypeDef result;

  Serial_PutString("\n\rWaiting bin file... (press 'a' to abort)\n\r");
  result = Ymodem_Receive( &size );
  if (result == COM_OK)
  {
    HAL_Delay(1000);
//    Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
//    Serial_PutString(aFileName);
//    Int2Str(number, size);
//    Serial_PutString("\n\r Size: ");
//    Serial_PutString(number);
//    Serial_PutString(" Bytes\r\n");
//    Serial_PutString("-------------------\n");
    
    Serial_PutString("OK!\n\r");
//    Serial_PutString(aFileName);
//    Int2Str(number, size);
//    Serial_PutString("\n\rSize: ");
//    Serial_PutString(number);
//    Serial_PutString(" Bytes\r\n");
  }
//  else if (result == COM_LIMIT)
//  {
//    Serial_PutString("\n\rSerr!\n\r");
//  }
//  else if (result == COM_DATA)
//  {
//    Serial_PutString("\n\rVerr!\n\r");
//  }
//  else if (result == COM_ABORT)
//  {
//    Serial_PutString("\r\n\nAbt.\n\r");
//  }
  else
  {
    Serial_PutString("\n\rErr!");
  }
}

/**
  * @brief  Upload a file via serial port.
  * @param  None
  * @retval None
  */
//void SerialUpload(void)
//{
//  uint8_t status = 0;

//  Serial_PutString("\n\n\rSelect Receive File\n\r");

//  HAL_UART_Receive(&huart_iap, &status, 1, RX_TIMEOUT);
//  if ( status == CRC16)
//  {
//    /* Transmit the flash image through ymodem protocol */
//    status = Ymodem_Transmit((uint8_t*)APPLICATION_ADDRESS, (const uint8_t*)"UploadedFlashImage.bin", USER_FLASH_SIZE);

//    if (status != 0)
//    {
//      Serial_PutString("\n\rError Occurred while Transmitting File\n\r");
//    }
//    else
//    {
//      Serial_PutString("\n\rFile uploaded successfully \n\r");
//    }
//  }
//}





/**
  * @brief  跳转至用户程序执行
  * @param  None
  * @retval None
  * @note   2023/8/23/张剑勇
  */
void StartApp(void)
{
  SysTick->CTRL = 0;
//  HAL_DeInit();
//  HAL_NVIC_DisableIRQ(SysTick_IRQn);
//  HAL_NVIC_ClearPendingIRQ(SysTick_IRQn);
//  __disable_irq();
  
  /* execute the new program */
  JumpAddress = *(__IO uint32_t*) (APPLICATION_ENTRY_ADDRESS + 4);
  /* Jump to user application */
  JumpToApplication = (pFunction) JumpAddress;
  /* Initialize user application's Stack Pointer */
  __set_MSP(*(__IO uint32_t*) APPLICATION_ENTRY_ADDRESS);
  JumpToApplication();
}





/**
  * @brief  Display the Main Menu on HyperTerminal
  * @param  None
  * @retval None
  */
void Main_Menu(void)
{
  uint8_t key = 0;

  while (1)
  {

//    Serial_PutString("\r\n=== Main Menu ===\r\n\n");
//    Serial_PutString("  Download - 1\r\n\n");
//    Serial_PutString("  Exe app  - 3\r\n\n");
//    Serial_PutString("=================\r\n\n");
    Serial_PutString("Press 1 or 3\r");
    
    /* Clean the input path */
    __HAL_UART_FLUSH_DRREGISTER(&huart_iap);
    __HAL_UART_CLEAR_OREFLAG(&huart_iap);
	
    /* Receive key */
    HAL_UART_Receive(&huart_iap, &key, 1, RX_TIMEOUT);

    switch (key)
    {
      case '1' :
        /* Download user application in the Flash */
        SerialDownload();
        break;
      
      case '3' :
        Serial_PutString("\n\rStart app\r\n");
        StartApp();
        break;
      
      default:
//        Serial_PutString("Press 1 or 3\r");
      break;
    }
  }
}

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
